package net.qiqb.module.spring;

import net.qiqb.module.ModuleContext;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.cglib.proxy.Proxy;

public class ModuleApiFactoryBean<T> implements FactoryBean<T> {

    private Class<T> apiInterface;

    private ModuleContext moduleContext;

    public ModuleApiFactoryBean(Class<T> apiInterface) {
        this.apiInterface = apiInterface;
    }

    @Override
    public T getObject() throws Exception {
        Object proxy = Proxy.newProxyInstance(
                ModuleApiFactoryBean.class.getClassLoader(),
                new Class[]{apiInterface},
                new ModuleApiProxyHandler(apiInterface,moduleContext)
        );
        return (T) proxy;
    }

    @Override
    public Class<?> getObjectType() {
        return this.apiInterface;
    }

    public void setApiInterface(Class<T> apiInterface) {
        this.apiInterface = apiInterface;
    }

    public void setModuleContext(ModuleContext moduleContext) {
        this.moduleContext = moduleContext;
    }
}
